package com.app.twth.ui.main;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.ViewPager;

import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.base.BaseFragment;
import com.app.twth.databinding.ActivityMainBinding;
import com.app.twth.statusbar.ScreenUtils;
import com.app.twth.ui.home.HomeFragment;
import com.app.twth.ui.mine.MineFragment;
import com.app.twth.ui.task.TaskFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private String TAG = getClass().getSimpleName();
    protected Activity mContext;
    private ActivityMainBinding binding;
    private int index = 0;
    private MainViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        ScreenUtils.setStatusBarTransparentAndWordsGray(getWindow());
        setContentView(binding.getRoot());
        EventBus.getDefault().register(this);
        initData();
    }


    private void initData() {
        viewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(MainViewModel.class);
        binding.setViewModel(viewModel);
        viewModel.getPermission();

        List<BaseFragment> fragmentList = new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new TaskFragment());
        fragmentList.add(new MineFragment());

        MainFragmentPagerAdapter pagerAdapter = new MainFragmentPagerAdapter(getSupportFragmentManager());
        pagerAdapter.setData(fragmentList, null);
        binding.viewPager.setAdapter(pagerAdapter);
        binding.viewPager.setOffscreenPageLimit(3);//缓存当前界面每一侧的界面数
        binding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        binding.rbHome.setChecked(true);
                        break;
                    case 1:
                        binding.rbTask.setChecked(true);
                        break;
                    case 2:
                        binding.rbMine.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        binding.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_home:
                        index = 0;
                        break;
                    case R.id.rb_task:
                        index = 1;
                        break;
                    case R.id.rb_mine:
                        index = 2;
                        break;
                }
                Log.e(TAG, "onCheckedChanged:" + index);
                int currentItem = binding.viewPager.getCurrentItem();
                if (currentItem == index) {
                    return;
                }
                binding.viewPager.setCurrentItem(index);
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainThread(Object object) {
        if (object instanceof String) {
            String str = (String) object;
            Log.e(TAG, "str:" + str);
            if (str.equalsIgnoreCase("logout")) {
                AppRouter.setLogin(this);
            }
        }
    }
}
